package org.tbjd.client.guis.projectSwitcher;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.ProjectsGridConstants;
import org.tbjd.client.customWidgets.CustomGrid;
import org.tbjd.client.dndControllers.projectSwitcher.GridRowPickupDragController;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.images.projectSwitcher.grid.GridImageBundle;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.Displayer;
import org.tbjd.client.interfaces.HasDragController;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.interfaces.Observer;
import org.tbjd.client.mediators.AbstractMediator;

import com.allen_sauer.gwt.dnd.client.DragController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class ProjectsGridGui extends Composite implements Displayer, Observer, HasMediator, Decisionmaker, Builder, HasDragController {

	// TODO change cursor style when hovering rows
	private static ProjectsGridGui instance = null;
	private SimplePanel container = new SimplePanel();

	private static final int NAME_COLUMN = 2;
	private AbstractMediator mediator = null;
	private Grid grid = null;
	private List<Widget> widgets = null;
	private int rows, columns;
	private GridRowPickupDragController dragController = null;
	private GridImageBundle gridImageBundle = (GridImageBundle) GWT.create(GridImageBundle.class);
	private int widgetsCount = 0;

	private ProjectsGridGui() {
		initWidget(container);
	}

	public static ProjectsGridGui getInstance() {
		if (instance == null) {
			instance = new ProjectsGridGui();
		}
		return instance;
	}

	public void build() {
	}

	public void decide(Object action) {
		System.out.println("ProjectsGridGui.decide(" + action + ")");

		if (((Map<?, ?>) action).containsKey(NotificationsConstants.OPEN_PROJECT)
				|| ((Map<?, ?>) action).containsKey(NotificationsConstants.DELETE_PROJECT)
				|| ((Map<?, ?>) action).containsKey(NotificationsConstants.TOGGLE_PRIORITY)
				|| ((Map<?, ?>) action).containsKey(NotificationsConstants.TOGGLE_STAR)) {
			try {
				delegate(action);
			} catch (NoUpperMediatorFoundException e) {
				e.toString();
			}
		}

	}

	public void delegate(Object action) throws NoUpperMediatorFoundException {
		System.out.println("ProjectsGridGui.delegate()");
		try {
			this.mediator.decide(action);
		} catch (TBJDException e) {
			e.printStackTrace();
		}
	}

	public void handle(Object action) {
	}

	public void display() {
		long begin = new Date().getTime();
	
		this.container.clear();

		if (this.widgetsCount > 0) {
			this.rows = widgets.size() / ProjectsGridConstants.COLUMNS;
			this.columns = ProjectsGridConstants.COLUMNS;

			this.grid = new CustomGrid(rows, columns);
			this.grid.setStyleName("table");

			this.container.add(grid);
		} else {

			Label noResultLabel = new Label("No results found !");
			noResultLabel.setStyleName("table_noResultFound");
			this.container.add(noResultLabel);
		}
		
		int widgetIndex = 0;

		if (this.widgetsCount > 0) {

			String[] columnsWidthPourcent = new String[] { "5%", "5%", "30%", "15%", "15%", "15%", "5%", "5%", "5%" };
			for (int i = 0; i < columnsWidthPourcent.length; i++) {
				this.grid.getColumnFormatter().setWidth(i, columnsWidthPourcent[i]);
			}

			Widget widget = null;
			for (int i = 0; i < rows; i++) {
				for (int j = 0; j < columns; j++) {
					widget = widgets.get(widgetIndex);
					this.grid.setWidget(i, j, widget);
					if (j == NAME_COLUMN) {
						HorizontalPanel namePanel = new HorizontalPanel();
						Image handle = gridImageBundle.handler_bullet_10x10().createImage();
						handle.setStyleName("table_handle");
						namePanel.add(handle);
						namePanel.add(widget);
						namePanel.setTitle("[" + i + "]");
						this.grid.setWidget(i, j, namePanel);
						this.dragController.makeDraggable(namePanel, handle);
					}
					widgetIndex++;
				}
				if (i % 2 == 0) {
					this.grid.getRowFormatter().setStyleName(i, "table_rowEven");
				} else {
					this.grid.getRowFormatter().setStyleName(i, "table_rowOdd");
				}
			}
		}
		System.out.println("ProjectsGridGui.display() "+(Long)(new Date().getTime()-begin)+"[ms]");
		
	}

	public AbstractMediator getMediator() {
		return this.mediator;
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.mediator.addObserver(this);
	}

	public void setDragController(DragController dragController) {
		this.dragController = (GridRowPickupDragController) dragController;
	}

	public Widget getBuildedWidget() {
		return ProjectsGridGui.getInstance();
	}

	public void update(Object sender) {
		System.out.println("ProjectsGridGui.update()");

		Map<String, List<Widget>> map = (Map<String, List<Widget>>) sender;
		if (map.containsKey(NotificationsConstants.DECORATED_PROJECTS)) {
			this.widgets = (List<Widget>) map.get(NotificationsConstants.DECORATED_PROJECTS);
			if (this.widgets != null) {
				this.widgetsCount = this.widgets.size();
				display();
			}
		}
	}

	public void display(List<?> updatedBeans) {
		// TODO Auto-generated method stub
		
	}

}
